package com.online.shopping.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.online.shopping.activemq.JmsConfig;
import com.online.shopping.custom.Goods;
import com.online.shopping.entity.RespBean;
import com.online.shopping.entity.ResultPage;
import com.online.shopping.pojo.TbGoods;
import com.online.shopping.pojo.TbItem;
import com.online.shopping.service.GoodsService;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.web.bind.annotation.*;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.Topic;
import java.util.List;

@RestController
@RequestMapping("/goods_ms")
public class GoodsController {

	@Autowired
	private GoodsService goodsService;

	@Autowired
	private JmsTemplate jmsTemplate;

	@PostMapping("/searchAndPage")
	public ResultPage searchAndPage(@RequestBody TbGoods searchEntity, int pageNum, int pageSize){
		System.out.println("页码："+pageNum);
		System.out.println("每页记录数："+pageSize);

		PageHelper.startPage(pageNum, pageSize);
		Page<TbGoods> page = (Page<TbGoods>) goodsService.search(searchEntity);

		ResultPage resultPage = new ResultPage();
		resultPage.setRows(page.getResult());
		resultPage.setTotal(page.getTotal());

		return resultPage;
	}

	@GetMapping("/findOne")
	public Goods findOne(Long id){
		System.out.println("id = "+id);
		return goodsService.findOne(id);
	}

	@PostMapping("/add")
	public RespBean add(@RequestBody Goods goods){
		System.out.println("进入Goods添加服务");
		try{
			String sellerId = "qiandu";     // 暂时设定
			goods.getGoods().setSellerId(sellerId);

			goodsService.add(goods);
			return RespBean.ok("添加成功！");
		} catch (Exception e){
			e.printStackTrace();
			return RespBean.error("添加失败！");
		}
	}

	@PostMapping("/update")
	public RespBean update(@RequestBody Goods goods){
		System.out.println("进入Goods修改服务");

		String sellerId = "qiandu";     // 暂时设定
		goods.getGoods().setSellerId(sellerId);

		Goods newGoods = goodsService.findOne(goods.getGoods().getId());
		if(!sellerId.equals(newGoods.getGoods().getSellerId()) || !sellerId.equals(goods.getGoods().getSellerId())){
			return RespBean.error("非法操作！");
		}

		try{
			goodsService.update(goods);
			return RespBean.ok("修改成功！");
		} catch (Exception e){
			e.printStackTrace();
			return RespBean.error("修改失败！");
		}
	}

	@PostMapping("/updateStatus")
	public RespBean updateStatus(@RequestBody Long[] ids, String status){
		System.out.println("ids：" + ids);
		System.out.println("status：" + status);

		try{
			// 1、修改goods表中的商品状态
			goodsService.updateStatus(ids, status);

			// 2、导入商品信息到ActiveMQ消息中间件
			if("1".equals(status)){
				// 审核通过，修改item表中SKU信息的状态
				List<TbItem> itemList = goodsService.findItemListByGoodsIdListAndStatus(ids, status);

				// 转换为json传输
				final String jsonString = JSON.toJSONString(itemList);
				System.out.println("Goods conroller: " + jsonString);

				// 将数据导入到Solr
				Topic topicSolrDestination = new ActiveMQTopic(JmsConfig.TOPIC_SOLR);
				jmsTemplate.send(topicSolrDestination, new MessageCreator() {

					@Override
					public Message createMessage(Session session) throws JMSException {
						return session.createTextMessage(jsonString);
					}
				});

				// 导入数据生成商品详情页
				for(final Long goodsId:ids){
					Topic topicPageDestination = new ActiveMQTopic(JmsConfig.TOPIC_HTML);
					jmsTemplate.send(topicPageDestination, new MessageCreator() {

						@Override
						public Message createMessage(Session session) throws JMSException {
							return session.createTextMessage(goodsId+"");
						}
					});
				}
			}

			return RespBean.ok("修改状态成功！");
		} catch (Exception e){
			e.printStackTrace();
			return RespBean.error("修改状态失败！");
		}
	}

	@PostMapping("/delete")
	public RespBean delete(@RequestBody Long[] ids){
		System.out.println("id[]: "+ids);

		try{
			goodsService.delete(ids);
			return RespBean.ok("删除成功！");
		} catch (Exception e){
			e.printStackTrace();
			return RespBean.error("删除失败！");
		}
	}
}
